package com.addcn.pushlibrary.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;

/**
 * author:luwenbo
 * created on date : 2019-05-14
 * content:
 * describe:
 */
public class LRomUtil {
    //用户实际rom，初始值设为其他类型（即使用极光推送）
    public static LRomUtil.ROM_TYPE userType = ROM_TYPE.JPush;
    //用户IMEI
    private static String userIMEI = null;
    private static String ROM_TAG = "LRomUtil";
    /**
     * 准备  设置IMEI，和用户ROM，必须最先调用
     *
     * @param context
     */
    public static void prepare(Context context) {

        LBuildProperties.loadProperties(context);
        //setIMEI(context);
        setRomType(context);
        Log.i(ROM_TAG, "rom prepare finish");
    }

    /**
     * 初始化用户手机类别
     */
    public static void setRomType(Context context) {
//        if (isHuawei(context)) {
//            userType = ROM_TYPE.Huawei;
//        } else if (isMi()) {
//            userType = ROM_TYPE.Mi;
//        } else if (isOppo()) {
//            userType = ROM_TYPE.OPPO;
//        } else if (isVivo()) {
//            userType = ROM_TYPE.VIVO;
//        } else if (isFcm(context)) {
//            userType = ROM_TYPE.FCM;
//        } else {
//            userType = ROM_TYPE.JPush;
//        }
        if (isFcm(context)) {
            userType = ROM_TYPE.FCM;
        } else {
            userType = ROM_TYPE.JPush;
        }
        Log.i(ROM_TAG, "userType:"+userType);
    }

    /**
     * 获取设备类型
     *
     * @return 设备类型：xiaomi huawei baidu
     */
    public static ROM_TYPE getRomType() {
        return userType;
       // return ROM_TYPE.OPPO;
        //return ROM_TYPE.Mi;
       // return ROM_TYPE.VIVO;
    }

    /**
     * 获得设备的IMEI号，并初始化其值
     *
     * @param context
     */
    @SuppressLint("MissingPermission")
    public static void setIMEI(Context context) {
        try {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
            if (tm != null) {
                userIMEI = tm.getDeviceId();
            } else {
                userIMEI = null;
            }
        }catch (Exception e){
            e.printStackTrace();
            userIMEI = null;
        }
    }

    /**
     * @return 用户设备IMEI号
     */
    public static String getIMEI() {
        return userIMEI;
    }

    // 判断是否是mi
    public static boolean isMi() {
        if (LBuildProperties.containsKey(KEY_MIUI_VERSION_NAME)
                || LBuildProperties.containsKey(KEY_MIUI_INTERNAL_STORAGE)
                || LBuildProperties.containsKey(KEY_MIUI_VERSION_CODE)) {
            return true;
        }
        return false;
    }

    // 判断是否是Oppo
    public static boolean isOppo() {
        if (LBuildProperties.containsKey(KEY_OPPO_VERSION_NAME)) {
            return true;
        }
        return false;
    }

    // 判断是否是Vivo
    public static boolean isVivo() {
        if (LBuildProperties.containsKey(KEY_VIVO_VERSION_NAME)) {
            return true;
        }
        return false;
    }
    //判断是否是华为
    public static boolean isHuawei(Context context) {
        if (LBuildProperties.containsKey(KEY_EMUI_API_LEVEL)
                || LBuildProperties.containsKey(KEY_EMUI_CONFIG_HW_SYS_VERSION)
                || LBuildProperties.containsKey(KEY_EMUI_VERSION_CODE)) {
            return true;
        }
        return false;
    }

    //是否支持Google服务
    public static boolean isFcm(Context context) {
        GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
        int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(context);
        if(resultCode != ConnectionResult.SUCCESS) {
            return false;
        }
        return true;
    }

    //小米配置项
    private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
    private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
    private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";

    //OPPO配置项
    private static final String KEY_OPPO_VERSION_CODE = "OPPO";
    private static final String KEY_OPPO_VERSION_NAME = "ro.build.version.opporom";

    //vivo
    private static final String KEY_VIVO_VERSION_CODE = "VIVO";
    private static final String KEY_VIVO_VERSION_NAME = "ro.vivo.os.version";

    //华为配置项
    private static final String KEY_EMUI_VERSION_CODE = "ro.build.version.emui";
    private static final String KEY_EMUI_API_LEVEL = "ro.build.hw_emui_api_level";
    private static final String KEY_EMUI_CONFIG_HW_SYS_VERSION = "ro.confg.hw_systemversion";

    public enum ROM_TYPE {Mi, Huawei, FCM,JPush,OPPO,VIVO}

}
